<?php
namespace app\wadmin\controller;
// +----------------------------------------------------------------------
// | bbcThink
// +----------------------------------------------------------------------
// | Copyright (c) 2025 http://bbcweb.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: bbcweb <22745529@qq.com>
// +----------------------------------------------------------------------
use app\BaseController;
use think\facade\View;
use think\facade\Db;

class Index extends BaseController
{      
    public function initialize()
    {
        parent::initialize();
    }
    public function index()
    {
        $user = $this->admin;
        if($user['id']>1)
        {
            //$url='/xadmin/index/adminpanel';
            //redirect($url)->send();
        }
        $menu = array();
        $menu_id=array();
        if($user['id']==1)
        {
            
            $menu = Db::table('lb_menu')->where(['pid'=>0,'status'=>1])->order('sort')->select()->toArray();
        }
        else
        {
            $role_id=explode(',',$user['role_id']);
            $menu_id=Db::table('lb_role_auth')->where('role_id','in',$role_id)->group('menu_id')->column('menu_id');
            //echo json_encode($menu_id);die;
            $menu=Db::table('lb_menu')->alias('pm')->where('pm.id','in',$menu_id)->where(['pm.pid'=>0])->order('sort')
            ->field('pm.*')
            ->select()->toArray();   
            
        }
        $i=0;
       
        foreach($menu as &$v)
        {
            $v['url']='/'.$v['appname'].'/'.$v['controller'].'/'.$v['method'];
            if(!empty($v['params']))
            {
                $v['url']=$v['url'].'?'.$v['params'];
            }
            if($i==0)
            {
                $v['is_active']=1;
            }
            else
            {
                $v['is_active']=0;
            }
            $i++;
            $child_menu=array();
            $where=array();
            if($user['id']!=1)
            {
                $where[]=['id','in',$menu_id];
            }
            $child_menu =  Db::table('lb_menu')->where(['pid'=>$v['id'],'status'=>1])->where($where)->order('sort')->select()->toArray();
            if($v['id']==83)
            {
                $child_menu = Db::name('menu')->alias('m')->join('RoleAuth ra','ra.menu_id=m.id')->where('ra.role_id',11)->where('m.pid','>',0)->field('m.*')->select()->toarray();
            }
            foreach($child_menu as &$vv)
            {
                $vv['url']='/'.$vv['appname'].'/'.$vv['controller'].'/'.$vv['method'];
                if(!empty($vv['params']))
                {
                    $vv['url']=$vv['url'].'?'.$vv['params'];
                }
            }
            $v['child_menu']=$child_menu;
        }
        
        View::assign('menu',$menu);      
        View::assign('user',$user);
        $lang=\app\service\CountryService::somelist(['where'=>[]]);
        $country=[];
        $lang_list=[];
        foreach ($lang as $lv)
        {
            if($lv['id']==cache('lang_id'))
            {
                $country=$lv;
            }
            else{
                $lang_list[]=$lv;
            }
        }
        
        View::assign('country',$country);
        View::assign('lang',$lang_list);
        return View::fetch();
        
        
    }
     //获取左侧菜单
     public function leftmenu()
     {
         $p=input();
         $menu=Db::name('menu')->where(['pid'=>$p['id']])->select()->toArray();
         foreach($menu as &$v)
         {
             $v['child']=Db::name('menu')->where(['pid'=>$v['id'],'status'=>1])->select()->toArray();
             foreach($v['child'] as &$vv)
             {
                 $vv['links']='/'.$vv['appname'].'/'.$vv['controller'].'/'.$vv['method'].'?'.$vv['params'];
             }
         }
         return json(['code'=>1,'data'=>$menu]);
    }
    public function setlang()
    {
        $p=input();
        cache('lang_id',$p['id']);
        jsonRes(1);
    }
    public function tongji()
    {
        $where=array();
        $day=date('Y-m-d');
        
        $total['article_count']=Db::name('article')->where(['delete_time'=>0])->count();
        View::assign('total',$total);
        return View::fetch();
    }
}
